package com.example.ktapplication.adapter

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.ktapplication.AppGlobals
import com.example.ktapplication.R
import com.example.ktapplication.activity.MainActivity
import com.example.ktapplication.bean.GuideBean

class GuidePageAdapter(): RecyclerView.Adapter<GuidePageAdapter.GuideViewHolder>() {

    // 在这里定义你的数据源
    private val dataList: MutableList<GuideBean> = mutableListOf()
    lateinit var activity: Activity;

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GuideViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.guide_item_view,
            parent, false)
        return GuideViewHolder(view)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: GuideViewHolder, position: Int) {
        val data = dataList[position]
        var visi:Boolean=false
        if (position+1==dataList.size){
            visi=true
        }
        holder.bindView(data,visi,activity)
    }

    // 添加数据到数据源
    fun setData(data: List<GuideBean>,act: Activity) {
        dataList.clear()
        dataList.addAll(data)
        activity=act;
        notifyDataSetChanged()
    }

    class GuideViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){

        val image: ImageView = itemView.findViewById(R.id.image)
        val btn: LinearLayout = itemView.findViewById(R.id.btn)
        fun bindView(bean: GuideBean,visi:Boolean,act: Activity) {
            Glide.with(AppGlobals.getApplication()).load(bean.url).into(image)
            if (visi){
                btn.visibility=View.VISIBLE;
                btn.setOnClickListener(OnClickListener{
                    val intent: Intent = Intent(
                        act,
                        MainActivity::class.java
                    )
                    act.startActivity(intent)
                    act.finish()
                })
            }
        }
    }

}

